package com.pengtu.web.human;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.User;
import com.pengtu.entity.history.HistoryProject;
import com.pengtu.entity.human.Leave;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.FProjectManager;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.history.HistoryProjectManager;
import com.pengtu.service.human.LeaveOvertimeManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 请假记录操作
 * 
 * @author Administrator
 * 
 */
@Results({
		@Result(name = BaseActionSupport.RELOAD, location = "leave.action", type = "redirect"),
		@Result(name = "part0", location = "${ctx}/human/leave!login.action?id=${id}", type = "redirect"),
		@Result(name = Constants.OWN, location = "${ctx}/human/leave/history.action?key=${id}&pngFileName=leave.png&acc=1-2", type = "redirect"),
		@Result(name = Constants.LAST, location = "${ctx}/human/leave!taketask.action?id=${id}&taskid=${taskid}", type = "redirect"),
		@Result(name = "part1", location = "${ctx}/human/leave!first.action?id=${id}&taskid=${taskid}", type = "redirect"),
		@Result(name = "part2", location = "${ctx}/human/leave!second.action?id=${id}&taskid=${taskid}", type = "redirect"),
		@Result(name = "part3", location = "${ctx}/human/leave!third.action?id=${id}&taskid=${taskid}", type = "redirect")})
public class LeaveAction extends BaseActionSupport<Leave> {

	private static final long serialVersionUID = 1L;

	private Leave leave;
	private String id;
	private String taskid;
	private LeaveOvertimeManager leaveOvertimeManager;
	private FProjectManager fProjectManager;
	private HistoryProjectManager historyProjectManager;
	private Attachment attachmention;
	private SystemManager systemManager;
	private Page<Leave> page = new Page<Leave>(10);

	private User user = SpringSecurityUtils.getCurrentUser();

	@Override
	public Leave getModel() {
		// TODO Auto-generated method stub
		return leave;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/**
	 * taskid
	 * 
	 * @return the taskid
	 * @since 1.0.0
	 */

	public String getTaskid() {
		return taskid;
	}

	/**
	 * @param taskid
	 *            the taskid to set
	 */
	public void setTaskid(String taskid) {
		this.taskid = taskid;
	}

	// public LeaveManager getLeaveManager() {
	// return leaveManager;
	// }
	//
	//
	//
	// public void setLeaveManager(LeaveManager leaveManager) {
	// this.leaveManager = leaveManager;
	// }
	/**
	 * @param fProjectManager
	 *            the fProjectManager to set
	 */
	@Autowired
	public void setfProjectManager(FProjectManager fProjectManager) {
		this.fProjectManager = fProjectManager;
	}

	/**
	 * @param historyProjectManager
	 *            the historyProjectManager to set
	 */
	@Autowired
	public void setHistoryProjectManager(
			HistoryProjectManager historyProjectManager) {
		this.historyProjectManager = historyProjectManager;
	}

	@Autowired
	public void setLeaveOvertimeManager(
			LeaveOvertimeManager leaveOvertimeManager) {
		this.leaveOvertimeManager = leaveOvertimeManager;
	}

	public Attachment getAttachmention() {
		return attachmention;
	}

	public void setAttachmention(Attachment attachmention) {
		this.attachmention = attachmention;
	}

	public SystemManager getSystemManager() {
		return systemManager;
	}

	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

	public Page<Leave> getPage() {
		return page;
	}

	public void setPage(Page<Leave> page) {
		this.page = page;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		if (!page.isOrderBySetted()) {
			page.orderBy("createDate");
			page.order(Page.DESC);
		}
		page = leaveOvertimeManager.findPageLeave(page, queryfilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		leaveOvertimeManager.saveLeave(leave);
		if (null != attachmention) {
			attachmention.setTableId(leave.getId());
			attachmention.setFileName("leave");
			attachmention.setTableField("leave_attachmention");
			systemManager.uploadFile(Struts2Utils.getAppRootPath(),
					attachmention);
			leaveOvertimeManager.saveLeave(leave);
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>();
		Set<Map.Entry<String, Map<String, Object>>> list = map.entrySet();
		for (Map.Entry<String, Map<String, Object>> entry : list) {
			entry.getValue().get("");
		}
		leaveOvertimeManager.deleteLeave(id);
		return RELOAD;
	}

	public String word() throws Exception {

		return Constants.WORD;
	}

	public void prepareWord() throws Exception {
		prepareModel();

	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			leave = leaveOvertimeManager.findPropertyLeave("id", id);
		} else {
			leave = new Leave();
		}

	}

	/**
	 * 启动请假流程 start: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String start() throws Exception {
		String deploymentId = fProjectManager.getByKey("leave")
				.getDeploymentId();
		String key = Struts2Utils.getParameter("key");
		String value = Struts2Utils.getParameter("value");
		String key1 = Struts2Utils.getParameter("key1");
		String value1 = Struts2Utils.getParameter("value1");
		String outcome = Struts2Utils.getParameter("outcome");
		Map<String, Object> variables = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(key)) {
			variables.put(key, value);
		}
		if (StringUtils.isNotEmpty(key1)) {
			variables.put(key1, value1);
		}
		if (StringUtils.isNotEmpty(outcome)) {
			variables.put("outcome", outcome);
		}
		variables.put("leaveId", id);
		leaveOvertimeManager.start(id, deploymentId, "leave", variables);
		return RELOAD;
	}

	public void prepareStart() throws Exception {
		prepareModel();
	}

	/**
	 * 保存 save1: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String save1() throws Exception {
		// TODO Auto-generated method stub
		leaveOvertimeManager.saveLeave(leave);
		if (null != attachmention) {
			attachmention.setTableId(leave.getId());
			attachmention.setFileName("leave");
			attachmention.setTableField("leave_attachmention");
			systemManager.uploadFile(Struts2Utils.getAppRootPath(),
					attachmention);
			leaveOvertimeManager.saveLeave(leave);
		}
		return "part0";
	}

	public void prepareSave1() throws Exception {
		prepareModel();
	}

	/**
	 * 启动页面 login: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String login() throws Exception {
		// TODO Auto-generated method stub
		return LOGIN;
	}

	public void prepareLogin() throws Exception {
		prepareModel();
	}

	public String assign() throws Exception {
		// TODO Auto-generated method stub
		return "assign";
	}

	public void prepareAssign() throws Exception {
		prepareModel();
	}
	
	public String first() throws Exception {
		// TODO Auto-generated method stub
		return "first";
	}

	public void prepareFirst() throws Exception {
		prepareModel();
	}
	
	public String second() throws Exception {
		// TODO Auto-generated method stub
		return "second";
	}

	public void prepareSecond() throws Exception {
		prepareModel();
	}
	
	public String third() throws Exception {
		// TODO Auto-generated method stub
		return "third";
	}

	public void prepareThird() throws Exception {
		prepareModel();
	}

	/**
	 * 业务转流程 judge: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String judge() throws Exception {
		taskid = leaveOvertimeManager.isPersonalTasks(user, id, "leaveId");
		if (StringUtils.isNotEmpty(taskid)) {
			return Constants.LAST;
		}
		return Constants.OWN;
	}

	public void prepareJudge() throws Exception {
		prepareModel();
	}

	/**
	 * 接受任务 taketask: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String taketask() throws Exception {
		HistoryProject historyProject = historyProjectManager.get(taskid);
		String activityName = leaveOvertimeManager.getTask(taskid)
				.getActivityName();
		if (historyProject == null) {
			historyProject = new HistoryProject();
			historyProject.setKey(id);
			historyProject.setUserName(user.getUserName());
			historyProject.setStartDate(new Date());
			historyProject.setTaskId(taskid);
			historyProject.setActivityName(activityName);
		}
		try {
			historyProjectManager.save(historyProject);
			leaveOvertimeManager.takeTask(taskid, user);
		} catch (org.jbpm.api.JbpmException e) {
			historyProjectManager.save(historyProject);
		}
		if(StringUtils.equals(activityName, "部门审核")){
			return "part1";
		}else if(StringUtils.equals(activityName, "分管领导审核")){
			return "part2";
		}else if(StringUtils.equals(activityName, "站长审核")){
			return "part3";
		}
		return null;
	}

	public void prepareTaketask() throws Exception {
		prepareModel();
	}
	
	/**
	 * 提交
	 * submit:
	 * 适用:
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public String submit() throws Exception {
		String outcome = Struts2Utils.getParameter("outcome");
		String view = Struts2Utils.getParameter("view");
		String key = Struts2Utils.getParameter("key");
		String value = Struts2Utils.getParameter("value");
		String key1 = Struts2Utils.getParameter("key1");
		String value1 = Struts2Utils.getParameter("value1");
		Map<String, Object> variables = new HashMap<String, Object>();
		HistoryProject historyProject = historyProjectManager.get(taskid);
		String activityName = leaveOvertimeManager.getTask(taskid)
				.getActivityName();
		System.out.println("++"+taskid);
		System.out.println("---"+historyProject);
		historyProject.setEndDate(new Date());
		historyProject.setMessage(user.getName() + "：" + view);
		if (StringUtils.isNotEmpty(key)) {
			variables.put(key, value);
		}
		if (StringUtils.isNotEmpty(key1)) {
			variables.put(key1, value1);
		}
		if(StringUtils.equals(activityName, "部门审核")){
			leave.setAuditor(user.getUserName());
			leave.setAuditDate(new Date());
			leave.setAuditview(view);
		}else if(StringUtils.equals(activityName, "分管领导审核")){
			leave.setVerifier(user.getUserName());
			leave.setVerifyDate(new Date());
			leave.setVerifyview(view);
		}else if(StringUtils.equals(activityName, "站长审核")){
			leave.setSigner(user.getUserName());
			leave.setSignview(view);
			leave.setSignDate(new Date());
		}
		leaveOvertimeManager.completeTask(taskid, outcome, variables);
		leaveOvertimeManager.saveLeave(leave);
		historyProjectManager.save(historyProject);
		return RELOAD;
	}
	
	public void prepareSubmit() throws Exception {
		prepareModel();
	}
}
